package servlet;

import bean.Users;
import service.impl.UsersServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        UsersServiceImpl usersService = new UsersServiceImpl();
        Users user = usersService.getUser(username, password);
        resp.setContentType("text/html;charset=utf-8;");
        PrintWriter writer = resp.getWriter();
        if (user!=null){
            req.getSession().setAttribute("user",user);
            req.getRequestDispatcher("index.jsp").forward(req,resp);

        }else {
        writer.println("<script>location.href = 'login.jsp';alert('请检查你的用户名或密码')</script>");
        }
    }
}
